<?php
/**
 *
 *
 * @author Iain Edminster
 */
class Bliss_Model_DbTable extends Zend_Db_Table_Abstract
{
	protected $_namespace = "Model_DbTable_";

	protected function _setupTableName()
	{
		if(!$this->_name) {
			$this->_name	= strtolower(str_replace($this->_namespace, "", get_class($this)));
		}
		parent::_setupTableName();
	}

	public function getMultiOptions($key, $value, $where=null, $order=null)
	{
		$query	= $this->select()->from($this, array(
			'key'	=> $key,
			'value'	=> $value
		));
		
		if(isset($where))	$query->where($where);
		if(isset($order))	$query->order($order);

		$all	= $this->fetchAll($query);
		$list	= array();
		foreach($all as $item) {
			$list[$item->key]	= $item->value;
		}

		return $list;
	}

	public function findOne($key, $value)
	{
		$all	= $this->findAll($key, $value);
		return count($all) ? $all[0] : null;
	}

	public function findAll(array $params = null, Zend_Db_Select $query = null)
	{
		$query	= isset($query) ? $query : $this->select();
		foreach($params as $field => $value) {
			if(strstr($value, '%')) {
				$query->where("{$field} LIKE ?", $value);
			} else {
				$query->where("{$field}=?", $value);
			}
		}

		return $this->fetchAll($query);
	}


}
?>